package cz.muni.fi.pa165.soccergamesarchive.validators;

import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.SoccerMatch;

import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCOMPLETE_RECORD_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCORRECT_ID_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NULL_VALUE_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.createErrorMessage;

/**
 *
 * @author Ondrej Dubovec
 */
public class SoccerMatchValidator {

    public static void validate(SoccerMatch match, ValidatorTest... tests)
            throws IllegalArgumentException {
        for (ValidatorTest test : tests) {
            switch (test) {
                case CORRECT_RECORD_TEST:
                    executeCorrectRecordTest(match);
                    break;
                case FULL_TEST:
                    executeFullTest(match);
                    break;
                case ID_TEST:
                    executeIdTest(match);
                    break;
                case NULL_TEST:
                    executeNullTest(match);
                    break;
                default:
                    throw new IllegalArgumentException(
                            "Required validation for SoccerMatch does not exist.");
            }
        }
    }

    private static void executeFullTest(SoccerMatch match) throws
            IllegalArgumentException {
        executeNullTest(match);
        executeIdTest(match);
        executeCorrectRecordTest(match);
    }

    private static void executeCorrectRecordTest(SoccerMatch match) throws
            IllegalArgumentException {
        if (match.getStadiumName() == null || match.getStadiumName().equals("")
                || match.getMatchDate() == null) {
            throw new IllegalArgumentException(createErrorMessage(
                    SoccerMatchValidator.class.getName(),
                    INCOMPLETE_RECORD_ERROR));
        }
    }

    private static void executeIdTest(SoccerMatch match) throws
            IllegalArgumentException {
        if (match.getId() == null || match.getId() < 1) {
            throw new IllegalArgumentException(createErrorMessage(
                    SoccerMatchValidator.class.getName(), INCORRECT_ID_ERROR));
        }
    }

    private static void executeNullTest(SoccerMatch match) throws
            IllegalArgumentException {
        if (match == null) {
            throw new IllegalArgumentException(createErrorMessage(
                    SoccerMatchValidator.class.getName(), NULL_VALUE_ERROR));
        }
    }

}
